package net.taviscaron.drivenotes.api;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Response util
 * @author Andrei Senchuk
 */
public class ResponseUtil {
    public static Object errorResponse (Integer code, String description) {
        ErrorResponse response = new ErrorResponse();
        response.setCode(code);
        response.setDescription(description);
        return statusResponse("error", "error", response);
    }

    public static Object errorResponse (ApiError error, String description) {
        return errorResponse(error.ordinal(), description);
    }

    public static Map<String, Object> multipleResponse(Object... args) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = (args.length % 2 == 0) ? args.length : (args.length - 1);
        for(int i = 0; i < count; i += 2) {
            String key = args[i].toString();
            Object value = args[i + 1];
            map.put(key, value);
        }
        return map;
    }

    public static Map<String, String> singleResponse(String param, String value) {
        return Collections.<String, String>singletonMap(param, value);
    }

    public static Map<String, Object> statusResponse(String status, Object... args) {
        Object[] params = new Object[args.length + 2];
        params[0] = "status";
        params[1] = status;
        System.arraycopy(args, 0, params, 2, args.length);
        return multipleResponse(params);
    }

    public static Map<String, Object> successResponse(Object... args) {
        return statusResponse("ok", args);
    }
}
